/**
 * left模块初始化
 * iframe 选项卡初始化分装
 */
var height = get_iframe_h();
var current_id = "iframe0";
var timer = null;
var menus = {};
menus["iframe0"] = "0";

var three_menu = {};

$(function(){

    // 初始化iframe高度
    $("#iframe0").css("height", height);

    $(".toggle").click(function(){
        $(".side").toggleClass("collapse");
    });

    init_module();	// 初始化菜单

    // 初始化 hove伪类
    // $("#onlyicon").hover(function () {
    //     $(this).addClass("console-component-sidebar-hover");
    // }).mouseleave(function () {
    //     $(this).removeClass("console-component-sidebar-hover");
    // })

})

/**
 * 创建iframe 选项卡
 * @return
 */
function init_iframe(_this, m_name, dataUrl) {
    var url = dataUrl;
    if (url.indexOf("?") != -1) {
        url = url + "&t=" + new Date().getTime();
    } else {
        url = url + "?t=" + new Date().getTime();
    }
    var iframe_name = 'iframe' + MODULE_ID;

    if (menus[iframe_name] && menus[iframe_name] == MODULE_ID) {
        //切换iframe
        var iframe = $("#" + iframe_name);
        changeIframe(iframe);
        iframe.attr("src", url);
        changeTabs($("." + iframe_name));
    } else {
        var iframe = createJqueryElement("iframe", iframe_name);
        iframe.css({"border": "0", "marginwidth": "0", "marginheight": "0", "width": "100%", "height": height, "frameborder": "no"});
        iframe.attr("src", url);
        iframe.attr("id", iframe_name);
        $(".mainContent").append(iframe);

        changeIframe(iframe);

        var menuTab = createJqueryElement('a');
        menuTab.addClass("J_menuTab").addClass(iframe_name);
        menuTab.html(m_name + "<i class=\"fa fa-times-circle\" onclick='delTab(this, event, 0)'></i>");
        menuTab.attr("onclick", "changeTab(this)");
        menuTab.attr("ondblclick", "delTab(this, event, 1)");
        menuTab.attr("iframe_id", iframe_name);
        menuTab.attr("title", m_name);
        $(".menuTabs").append(menuTab);
        changeTabs(menuTab);

        menus[iframe_name] = MODULE_ID;
        tabnav_w();
    }
    changeMenuClass(_this);
}

function createJqueryElement (type, iframe_name) {
    if (type == 'iframe') {
        var iframe = null;
        try {
            iframe = document.createElement('<iframe name="' + iframe_name + '" id="' + iframe_name + '"></iframe>');
        } catch (e) {
            iframe = document.createElement('iframe');
            iframe.name = iframe_name;
            iframe.id = iframe_name;
        }
        return $(iframe);
    }
    var obj = document.createElement(type);
    return $(obj);
}

function changeMenuClass (obj) {
    $(".menuChange ul li ").removeClass("active");
    $(obj).addClass("active");
}

function changeIframe (obj) {
    $(".mainContent iframe").hide();
    obj.show();
    current_id = obj.attr("id");
}

function changeTabs (obj) {
    $(".J_menuTab").removeClass("active");
    obj.addClass("active");
}

function changeTab (obj) {
    clearTimeout(timer);
    timer = setTimeout(function() {//初始化一个延时
        changeTabs($(obj));
        var iframe_id = $(obj).attr("iframe_id");
        var iframe = $("#" + iframe_id);
        MODULE_ID = iframe_id.replace("iframe", "");
        changeIframe(iframe);
    }, 200);
}

/**
 * 删除选项卡
 * @param obj
 * @param event
 * @param flag
 * @return
 */
function delTab (obj, event, flag) {
    clearTimeout(timer);
    var parent = $(obj);
    if (flag == 0) {//单击事件
        parent = $(obj).parent();
    }
    var iframe_id = parent.attr("iframe_id");
    if (iframe_id == current_id) {//当前的iframe,关闭时显示上一个
        if ($("#" + iframe_id).next().attr("id")) {//双击事件
            $("#" + iframe_id).next().show();
        } else {
            $("#" + iframe_id).prev().show();
        }
        if (parent.next().attr("iframe_id")) {//双击事件
            parent.next().addClass("active");
            current_id = parent.next().attr("iframe_id");
        } else {
            parent.prev().addClass("active");
            current_id = parent.prev().attr("iframe_id");
        }
        $("#" + iframe_id).remove();

    } else {
        $("#" + iframe_id).remove();
    }
    parent.remove();
    tabnav_w();
    menus[iframe_id] = "-1";
    event.stopPropagation();


}


/**
 * 初始化菜单模块
 * 一级，菜单模块
 */
function init_module(){

    This.jqueryAjax(Api.admin.base.baseModule.topModule, "", "", function(data){
        var html  = '<div class="sidebar-inner">';
        // html  = '	<div class="sidebar-fold icon-unfold"></div>';

        for (var i = 0; i < data.length; i++) {
            html += '	<div class="sidebar-nav" data-index="' + i + '" >';
            html += '		<div class="sidebar-title sidebar-trans  module_one_listener " data-id="' + data[i].moduleId + '" id="' + data[i].moduleId + '" >';
            html += '			<div class="sidebar-title-inner ">';
            if (data[i].style) {
                html += '	<span class="sidebar-title-icon ' + data[i].style + '"></span>';
            } else {
                html += '	<span class="sidebar-title-icon icon-arrow-right"></span>';
            }
            html += '				<span class="sidebar-title-text over_hidden">' + data[i].name + '</span>';
            html += '	<span class="layui-right layui-icon  layui-icon-right "></span>';

            html += '			</div>';

            html += '		</div>';
            html += '<div class="  erji' + i + '" style="display: none;"></div>'

            html += '	</div>';
        }

        html += '</div>';

        $(".console-component-sidebar").html(html);
    });

    $(".sidebar-inner .sidebar-nav ").each(function(){
        var id =  $(this).find(".module_one_listener").data("id");
        //  var self=$(this);
        // 加载三级模块
        module_tow(id,$(this).index());
    });


    /**
     * 二级 展开 三级模块 展开四级
     */
    $(".menuChange ul li>div.text").click( function() {
        if ($(this).parent().has("ul")) {
            $(this).siblings('ul').toggle();
            $(this).siblings('span').find("i").toggleClass("icon-arrow-down").toggleClass("icon-arrow-right");
        }
    })


}

/**
 * 二级 三级，四级模块
 */
function module_tow(id,i) {
    if (three_menu[id]) {
        $(".erji"+i ).html(three_menu[id]);
    } else {
        This.jqueryAjax(Api.admin.base.baseModule.childModule, {parentId: id}, "json", function(data){
            var html  = '<div class="menu menuChange" lay-filter="demo" lay-allowclose="true"><ul>';
            html += module_iteration(data);
            html += '</ul></div>';
            three_menu[id] = html;
            $(".erji" + i ).html(html);
        });
    }

}

// 二级菜单迭代
function module_iteration(data){
    var html = "";
    // 当前级
    for (var i = 0; i < data.length; i++) {
        var prev = null;
        if (data[i].childs && data[i].childs.length == 1) {
            //prev = data[i];
            // 当下级只有一个时把他当做当前级
            data[i] = data[i].childs[0];
        }

        html += '<li class="nav-item"';
        if (data[i].url && data[i].url != "#") {
            html += 'onclick="menuChange(this)"';
        }
        html += 'name="' + data[i].name + '" data-url="' + data[i].url + '" id="' + data[i].moduleId + '">';

        html += '<span class="icon">';
        if (data[i].childs && data[i].childs.length > 0) {
            html += '<i class="icon-arrow-right"></i>';
        }else{
           // html += '	<span class="' + data[i].style + '"></span>';
        }
        html += '</span>';
        html += '<div class="text text-' + data[i].moduleId + ' over_hidden" name="' + (prev ? prev.name + '-' : '') + data[i].name + '" title="' + (prev ? prev.name + '-' : '') + data[i].name + '">' + (prev ? prev.name + '-' : '') + data[i].name + '</div>';
        // 下一级
        if (data[i].childs && data[i].childs.length > 0) {
            var next_data = data[i].childs;
            html += '<ul style="display: none;">';
            // 自调
            html += module_iteration(next_data);
            html += '</ul>';
        }
        html += '</li>';
    }
    return html;

}

/**
 * 一级模块点击事件
 * 一级模块展开收起
 */





$("body").on('click', '.module_one_listener', function() {
    // 选中样式 sidebar-title-inner nav-item-active
 //   $(".sidebar-title-inner").removeClass("nav-item-active");
  //  $(this).find(".sidebar-title-inner").addClass("nav-item-active");
    $(this).find(".layui-icon").toggleClass("layui-icon-down");
    $(this).next().slideToggle();


})



/**
 * 箭头点击
 */
// $(".menuChange").on('click', 'ul li span', function() {
//     $(this).siblings('div.text').click();
// });


/**
 * 二级 三级 四级 模块新建窗口
 */
function menuChange (_this) {
    try {
        event.preventDefault();
    } catch (e) {

    }
    var m_url = $(_this).data("url");
    if (m_url && m_url != '#') {
        var m_name = $(_this).attr("name");
        MODULE_ID = $(_this).attr("id");
        init_iframe(_this, m_name, m_url);
    }
}

//顶部导航-我的消息
function topMoudleClick(array) {
    if (array.length == 3) {
        var one = array[0], two = array[1], three = array[2];
        $("#" + one)[0].click();
        $(".text-" + two)[0].click();
        $(".text-" + three)[0].click();
    } else if (array.length == 4) {
        var one = array[0], two = array[1], three = array[2], four = array[3];
        $("#" + one)[0].click();
        $(".text-" + two)[0].click();
        if ($(".menu #" + three).length > 0) {
            $(".text-" + three)[0].click();
        }
        if ($(".menu #" + four).length > 0) {
            $(".text-" + four)[0].click();
        }
    } else if (array.length == 2) {
        var one = array[0], two = array[1];
        $("#" + one)[0].click();
        $("#" + two)[0].click();
    }
}


function tabnav_w(){
    var w= $(".top-bar").width();
    var len=Math.floor(w/150);
    var wlen=$("#tabnav a").length;
    var pw=$(".top-bar").width();
    if(wlen>len){
        $("#tabnav").css("width","100%");
        $("#tabnav").css("table-layout","fixed") ;
        $("#tabnav a").css("width","auto");
    }else{
        $("#tabnav").css("width","auto");
        $("#tabnav").css("table-layout","") ;
        $("#tabnav a").css("width","150px");

    }
}
